﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;

using APX.Enums;

namespace Classes
{
   public class AlarmSystem
   {
      #region Private Member Variables

         private ControlUnit panel;
         private Connection  uplink;
         private String transformerLocation;
         private LineSeizure seizure;
         private Double points;

         private BindingList<Sensor> sensors;

      #endregion

      #region Properties

      public ControlUnit ControlUnit   { get { return panel; } }
      public Connection Uplink         { get { return uplink; } }
      public LineSeizure Seizure       { get { return seizure; } }
      public String Transformer        { get { return transformerLocation; } }
      public Double Points             { get {return points; } }
      public BindingList<Sensor> Sensors      { get { return sensors; } }
      public String SensorDBString
      {
         get
         {
            String x = "'";
            foreach (Sensor i in sensors)
            {
               x += i.DBString + ";";
            }

            x = x.TrimEnd(';');
            x += "'";

            return x;
         }
      }
      public String SystemDBString
      {
         get
         {
            return String.Format("'{0};{1};{2};{3}'", panel, uplink, transformerLocation, seizure);
         }
      }

      #endregion

      #region Constructors

      /// <summary>
      /// Creates a new AlarmSystem object with individual parameters.
      /// </summary>
      /// <param name="iPanel">Type of Control Panel</param>
      /// <param name="iConnection">Type of Connection to Monitoring</param>
      /// <param name="iSeizure">Level of Seizure on Phone Line</param>
      /// <param name="iTransformer">Location of power transformer</param>
      /// <param name="iSensors">list of Sensors installed</param>
      public AlarmSystem (String iPanel, String iConnection, String iSeizure, String iTransformer, 
         String iSensors)
      {
         panel = (ControlUnit)Enum.Parse(typeof (ControlUnit), iPanel);
         uplink = (Connection)Enum.Parse(typeof (Connection), iConnection);
         seizure = (LineSeizure)Enum.Parse(typeof (LineSeizure), iSeizure);
         transformerLocation = iTransformer;
         points = 0;
         sensors = new BindingList<Sensor>();
         
         foreach (String i in iSensors.Split( ';' ))
            sensors.Add( new Sensor( i ) );
      }

      /// <summary>
      /// Creates a new AlarmSystem object from a single string retrieved from the database.
      /// </summary>
      /// <param name="info">String containing all parameters.</param>
      public AlarmSystem (String info, String iSensors)
      {
         String[] i = info.Split( ';' );
         panel = (ControlUnit) Enum.Parse (typeof (ControlUnit), i[0]);
         uplink = (Connection) Enum.Parse (typeof (Connection), i[1]);
         transformerLocation = i[2];
         seizure = (LineSeizure) Enum.Parse (typeof (LineSeizure), i[3]);
         points = 0;
         sensors = new BindingList<Sensor>();

         foreach (String j in iSensors.Split( ';' ))
            sensors.Add(new Sensor(j));
      }

      #endregion
   }
}
